<?php
/**订单相关
 * Created by PhpStorm.
 * User: Wang YuLong
 * Date: 2018/11/28
 * Time: 8:51
 */

namespace app\api\controller;


use think\Controller;
use app\api\model\Indent;
use app\api\model\Userticket;

class Indents extends Controller
{
    /**
     * 用户查询订单信息
     */
    public function index()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost())
        {
            $userid = input('id');                //用户id
            $status   = input('status');    //查询订单的状态

            if ($status == 1){  $statuss=4; }
            if ($status == 2){  $statuss=5; }
            if ($status == 3){  $statuss=2; }
            if ($status == 4){  $statuss=['in',('0,1')]; }
            $info = [];
            $info['userid'] = $userid;
            $info['audit'] = $statuss;

            $indent = new Indent();
            $data = $indent->index($info);
            if($data == 0)
            {
                AjaxReturn('10019','暂无订单信息');
            }
            else
            {
                AjaxReturn('1','成功',['details'=>$data]);
            }
        }
    }

    /**
     * 生成单个商品订单
     */
    public function add()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost())
        {
            //判断有无优惠券
            //生成订单信息
            $userid = input('userid');  //接收用户id
            $issueid = input('issueid'); //商品id
            $money=abs(intval(input('number'))*floatval(input('money')));//订单金额=数量*价格
            $userticket=new Userticket();
            $data=$userticket->OneData($userid,$issueid,$money);
            $res=['dnumber'=>input('dnumber'),
                'userid'=>$userid,
                'issueid'=>$issueid,
                'agencyid'=>input('shopid'),
                'price'=>abs(floatval(input('money'))),
                'number'=>abs(intval(input('number'))),
                'ticketid'=>$data[1],
                'reality'=>$data[0],
                'times'=>date('Y-m-d H:i:s',time()),
                'audit'=>5,
                'window'=>0,
                'goodscode'=>rand(100,999).rand(100,999)
            ];
            $indent = new Indent();
            $datas = $indent->adds($res);
            AjaxReturn('1','成功',['details'=>$datas]);
        }
    }
    /**
     * 取消订单
     */
    public function del(){
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost())
        {
            $indentid = input('indentid');
            $indent = new Indent();
            $data = $indent->OneData($indentid);
            $userticket=new Userticket();
            $arr=$userticket->qx($data['ticketid']);
            $indent->where('id',$indentid)->delete();
            AjaxReturn('1','成功');
       }
    }
    /**
     * 统计用户的订单总数
     */
    public function countindent()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost())
        {
            $info = [];
            $info['userid'] = input('id');  //接收用户id
            $type = input('type'); //接收查询订单的类型
            if($type == 1){ $info['audit'] =5; }
            if($type == 2){ $info['audit'] =['in',('2,3')]; }
            if($type == 3){ $info['audit'] =1; }

            $indent = new Indent();
            $data = $indent->countindent($info);
            AjaxReturn('1','成功',['details'=>$data]);
        }
    }

}